import { JKManager } from "../JKManager";
import { DataUpdateTool } from "../Utils/DataUpdateTool";
import { TaskQueueTool } from "../Utils/TaskQueueTool";

const { ccclass, property } = cc._decorator;

export type FloatData = {
    floatViewPaths: string[];
    floatViewDatas: unknown[];
}

/**
 * 悬浮界面组件基类
 */
@ccclass
export class FloatViewBase extends cc.Component {
    protected _dataUpdateTool: DataUpdateTool;
    protected _taskQueueTool: TaskQueueTool;

    onLoad(data: FloatData = null): void {
        cc.log(`${cc.js.getClassName(this)} onLoad data = `, data)

        this._dataUpdateTool = new DataUpdateTool()
        this._taskQueueTool = new TaskQueueTool()
    }

    /**
     * 业务初始化，与 close 成对
     * @param first 首次初始化
     */
    init(data: unknown, first: boolean) {
        cc.error(`${cc.js.getClassName(this)} You have to overide function "init" in your view component!`)
    }

    update(dt: number): void {
        this._taskQueueTool.doCmd()
    }

    /**
     * 业务清理，与 init 成对
     */
    clear() {
        cc.error(`${cc.js.getClassName(this)} You did not overide function "clear" in your view component!`)
    }

    close() {
        cc.warn(`You did not overide function "close" in your view component!`)
    }

    // ====================================================================================
    // ===================================== 内部接口 ======================================
    // ====================================================================================



    // ====================================================================================
    // ===================================== 外部接口 ======================================
    // ====================================================================================

    /**
     * 刷新悬浮窗界面
     * @param data 数据
     */
    refreshFloatView(data: FloatData): void {
        cc.log(`${cc.js.getClassName(this)} refreshFloatView data = `, data)

        data = data || {
            floatViewPaths: [],
            floatViewDatas: [],
        }
        let floatViewPaths = data.floatViewPaths,
            floatViewDatas = data.floatViewDatas,
            openFloatViewPaths = JKManager.UiManager.getFloatViewPaths()
        openFloatViewPaths.forEach(openFloatViewPath => {
            floatViewPaths.indexOf(openFloatViewPath) < 0 && JKManager.UiManager.closeFloatView(openFloatViewPath)
        })
        floatViewPaths.forEach((floatViewPath, index) => {
            openFloatViewPaths.indexOf(floatViewPath) < 0 && JKManager.UiManager.showFloatView(floatViewPath, floatViewDatas[index])
            let floatView = JKManager.UiManager.getFloatView(floatViewPath)
            floatView instanceof cc.Node && floatView.setSiblingIndex(index)
        })
    }

    /**
     * 获取界面动画执行目标节点
     */
    getViewActionTargetNode() {
        cc.warn(`${cc.js.getClassName(this)} You did not overide function "getViewActionTargetNode" in your view component!`)
    }
}